package com.haierCamera.customapplication.ui.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;


import com.haierCamera.customapplication.utils.DensityUtil;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author yawei
 * @data on 2018/7/27 上午10:48
 * @email zyawei@live.com
 */
public class GridLayoutColorDivider extends RecyclerView.ItemDecoration {

    private Drawable mDivider;
    private int dividerHeight = 1;
    private int decorationHeight = 1;

    public GridLayoutColorDivider(Context context) {
        this(context, 0xffF8F8F8, 1, 1);
    }

    public GridLayoutColorDivider(Context context, @ColorInt int color, int decorationHeight, int dividerHeight) {
        this.mDivider = new ColorDrawable(color);
        this.dividerHeight = DensityUtil.dip2px(context, dividerHeight);
        this.decorationHeight = DensityUtil.dip2px(context, decorationHeight);
        if (dividerHeight > decorationHeight) {
            throw new IllegalArgumentException("dividerHeight [" + dividerHeight + "] > decorationHeight[" + decorationHeight + "]");
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int left = 0, right = 0, top = 0, bottom = 0;
        int lineSpace = decorationHeight / 2 > 1 ? decorationHeight / 2 : 1;
        int adapterPosition = parent.getChildAdapterPosition(view);
        int itemCount = parent.getAdapter().getItemCount();
        int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        int location = adapterPosition % spanCount;
        if (location == 0) {
            right = lineSpace;
            top = lineSpace;
            bottom = lineSpace;
        } else if (location == spanCount - 1) {
            left = lineSpace;
            top = lineSpace;
            bottom = lineSpace;
        } else {
            left = lineSpace;
            right = lineSpace;
            top = lineSpace;
            bottom = lineSpace;
        }
        if (adapterPosition < spanCount) {
            top = 0;
        } else if (adapterPosition < itemCount - spanCount) {
            bottom = 0;
        }
        Log.i("TAG","left-" + left + "/top-" + top + "/right-" + right + "/bottom-" + bottom);
        outRect.set(left, top, right, bottom);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int top = parent.getPaddingTop();
        int bottom = parent.getHeight() - parent.getPaddingBottom();


        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        int lineSpace = decorationHeight / 2 > 1 ? decorationHeight / 2 : 1;
        int itemCount = parent.getAdapter().getItemCount();
        int childCount = parent.getChildCount();
        int halfDevider = dividerHeight / 2;
        //draw ver
        for (int i = 1; i < spanCount; i++) {

            int x = (right - left) / spanCount * i;
            int l;
            int r;
            if (halfDevider > 1) {
                l = x - halfDevider;
                r = x + halfDevider;
            } else {
                l = x + left;
                r = l + dividerHeight;
            }
            //Timber.i("x = " + x + "l = " + l + " r = " + r+" width = " + parent.getWidth()+" left-right = " + (right-left));
            mDivider.setBounds(l, top, r, bottom);
            mDivider.draw(c);
        }
        //draw hor
        for (int i = 0; i < childCount; i++) {
            if (i % spanCount == 0) {
                final View child = parent.getChildAt(i);
                int adapterPosition = parent.getChildAdapterPosition(child);
                if (adapterPosition < itemCount - spanCount) {
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                    top = child.getBottom() + params.bottomMargin + lineSpace - halfDevider;
                    bottom = top + dividerHeight;
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);

                }
            }
        }
    }


}
